import NProgress from 'nprogress';

const commonFn = {
    j2s(obj) {
        return JSON.stringify(obj)
    },
    putCacheData(key, value ,timeout){
        let _value = {};
        _value.value = value;
        if(timeout){
            _value.timeout = timeout;
            _value.timestamp = new Date().getTime();
        }
        store.dispatch('setCacheData', {key : key , value : _value});
    },
    getCacheData(key){
        let _value = store.state.cacheData[key];
        if(_value && _value.timeout){
            let cur = new Date().getTime();
            let diff = cur - _value.timestamp;
            if(diff > _value.timeout){
                this.removeCacheData(key);
                return null;
            }else{
                return _value.value;
            }
        }else if(_value){
            return _value.value;
        }else{
            return null;
        }
    },
    removeCacheData(key){
        let value = store.state.cacheData[key];
        if(value){
            store.dispatch('removeCacheData',key);
        }
        return value;
    },
    closeGlobalLoading() {
        // setTimeout(() => {
        //     store.dispatch('showLoading', false)
        // }, 0)
        NProgress.done();
    },
    openGlobalLoading() {
        NProgress.start();
        // setTimeout(() => {
        //     store.dispatch('showLoading', true)
        // }, 0)
    },
    cloneJson(obj) {
        return JSON.parse(JSON.stringify(obj))
    },

    toastMsg(type, msg) {
        switch (type) {
            case 'normal':
                bus.$message(msg)
                break
            case 'success':
                bus.$message({
                    message: msg,
                    type: 'success'
                })
                break
            case 'warning':
                bus.$message({
                    message: msg,
                    type: 'warning'
                })
                break
            case 'error':
                bus.$message.error(msg)
                break
        }
    },
    clearVuex(cate) {
        store.dispatch(cate, [])
    },
    getHasRule(val) {
        const moduleRule = 'admin'
        let userInfo = Lockr.get('userInfo')
        if (userInfo.id == 1) {
            return true
        } else {
            let authList = moduleRule + Lockr.get('authList')
            return _.includes(authList, val)
        }
    },
    getObjectURL(file) {
        var url = null
        // 下面函数执行的效果是一样的，只是需要针对不同的浏览器执行不同的 js 函数而已
        if (window.createObjectURL != undefined) { // basic
            url = window.createObjectURL(file)
        } else if (window.URL != undefined) { // mozilla(firefox)
            url = window.URL.createObjectURL(file)
        } else if (window.webkitURL != undefined) { // webkit or chrome
            url = window.webkitURL.createObjectURL(file)
        }
        return url
    },
    // 获得树状节点前三级的节点数组集合
    getTreeNodeById(tree,val,key){
        let _temp = '';
        function findById(oTree,val,key){      
            for(let i=0;i<oTree.length;i++){
                let item = oTree[i];
                if(item[key] && item[key] == val){  
                    _temp =  item;
                }else{
                    if(item.subList && item.subList.length>0){
                        findById(item.subList,val,key);
                    }
                }
            }

        }
        findById(tree,val,key);
        return _temp;
    },

    getUnfoldNodes(tree) {
        let arr = []
        let index = 0

        function pushNode(oTree, array) {
            index++
            oTree.forEach((item) => {
                array.push(item.id)
                if (item.subList.length > 0 && index < 3) {
                    pushNode(item.subList, array)
                }
            })
        }

        pushNode(tree, arr)
        return arr
    },
    // 获取当前经纬度所在组织机构的数组集合
    getCheckedNodes(tree, lng, lat) {
        let arr = []
        let point = new BMap.Point(lng, lat)
        function findId(tree, point, arr) {
            tree.forEach((item) => {
                if (item.position) {
                    let pointArr = []
                    item.position.split(',').forEach((val) => {
                        pointArr.push(new BMap.Point(val.split('_')[0], val.split('_')[1]))
                    })
                    if (BMapLib.GeoUtils.isPointInPolygon(point, new BMap.Polygon(pointArr))) {
                        arr.push(item.id)
                    }
                }
                if (item.subList.length > 0) {
                    findId(item.subList, point, arr)
                }
            })
        }
        findId(tree, point, arr)
        return arr
    },
    // 根据id获取position
    getPositionForTree(tree, id) {
        function getPosition(list) {
            for (let i = 0; i < list.length; i++) {
                if (id === list[i].id) {
                    return list[i].position
                } else {
                    if (list[i].subList.length > 0) {
                        let a = getPosition(list[i].subList)
                        if (a) {
                            return a
                        }
                    } else {
                        return ''
                    }
                }
            }
        }

        return getPosition(tree)
    },
    // 根据id获取城市名称
    getNameForTree(tree, id) {
        function getName(list) {
            for (let i = 0; i < list.length; i++) {
                if (id === list[i].id) {
                    return list[i].name
                } else {
                    if (list[i].subList.length > 0) {
                        let a = getName(list[i].subList)
                        if (a) {
                            return a
                        }
                    } else {
                        return ''
                    }
                }
            }
        }

        return getName(tree)
    }
}

export default commonFn
